package com.sugon.xuanyuan;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;

import java.util.List;
import java.util.Properties;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws AnalysisException {
        //1. 创建配置对象
        SparkConf conf = new SparkConf().setAppName("sparksql");

        //2. 获取sparkSession
        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

        //3. 编写代码


        // 添加参数
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","admin1234@sugon");

//        json.write()
//                // 写出模式针对于表格追加覆盖
//                .mode(SaveMode.Append)
//                .jdbc("jdbc:mysql://hadoop102:3306","gmall.testInfo",properties);

        Dataset<Row> jdbc = spark
                .read()
                .jdbc("jdbc:mysql://100.150.0.123:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true", "image", properties);


        jdbc.createOrReplaceTempView("images");

        Dataset<Row> jdbcLpr = spark
                .read()
                .jdbc("jdbc:mysql://100.150.0.123:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true", "lpr_image", properties);

        jdbcLpr.createOrReplaceTempView("lpr_images");
        spark.udf().register("ocr", new OcrUDF(), DataTypes.StringType);
        spark.udf().register("lpr", new LPRUDF(), DataTypes.StringType);

        Dataset<Row> retOfOcr = spark.sql("select name,ocr(image_data) as image_desc from images");
        retOfOcr.show(20,1000);
        Dataset<Row> retOfLpr = spark.sql("select name,lpr(image_data) as image_desc from lpr_images");
        retOfLpr.show(20,1000);

        //4. 关闭sparkSession
        spark.close();
    }
}
